package com.the4man.framework.service.impl;

import com.the4man.framework.cache.PermissionCache;
import com.the4man.framework.dao.MenuMapper;
import com.the4man.framework.dao.MoudleMapper;
import com.the4man.framework.model.Menu;
import com.the4man.framework.model.Moudle;
import com.the4man.framework.pagemodel.PMenu;
import com.the4man.framework.pagemodel.PUser;
import com.the4man.framework.service.MenuServiceI;
import com.the4man.framework.util.DateUtil;
import com.the4man.framework.utils.RequestUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by lenovo on 2017/12/12.
 */
@Service("menuService")
public class MenuServiceImpl implements MenuServiceI {
    private static final Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);
    private static final int String = 0;
    private static final int list = 0;
    @Autowired
    MenuMapper menuMapper;
    @Autowired
    MoudleMapper moudleMapper;
    @Autowired
    PermissionCache permissionCache;

    public MenuServiceImpl() {
    }

    public List<PMenu> getAllMenuByMoudleID(String moudleid) {
        List<PMenu> pMenus = new LinkedList();
        List<Menu> menus = this.menuMapper.selectMenusByMoudleID(moudleid);
        PUser user = RequestUtil.getSessionUserinfo();
        HashSet<String> permissions = this.permissionCache.getPermissionByRoleId(user.getRoleid());
        if(!StringUtils.equals(moudleid, "SYSTEM") && permissions == null) {
            return pMenus;
        } else {
            PMenu pMenu = null;
            Iterator var8 = menus.iterator();

            while(true) {
                Menu m;
                do {
                    do {
                        if(!var8.hasNext()) {
                            return pMenus;
                        }

                        m = (Menu)var8.next();
                    } while(m.getBasePermission().intValue() == 1);
                } while(!StringUtils.equals(moudleid, "SYSTEM") && !permissions.contains(m.getUrl()));

                pMenu = new PMenu();
                BeanUtils.copyProperties(m, pMenu);
                Map<String, Object> attributes = new HashMap();
                attributes.put("url", m.getUrl());
                attributes.put("loadtype", m.getMenuloadtype());
                pMenu.setAttributes(attributes);
                pMenus.add(pMenu);
            }
        }
    }

    public List<PMenu> getAllMenu() {
        List<Menu> menus = this.menuMapper.selectAllMenusForPage();
        List<PMenu> pMenus = new LinkedList();
        PMenu pMenu = null;
        Iterator var5 = menus.iterator();

        while(var5.hasNext()) {
            Menu m = (Menu)var5.next();
            System.out.println(m.getUrl());
            pMenu = new PMenu();
            BeanUtils.copyProperties(m, pMenu);
            pMenus.add(pMenu);
        }

        return pMenus;
    }

    public List<PMenu> getTreeMenu(String roleid) {
        List<Menu> menus = this.menuMapper.selectTreeMenu();
        List<PMenu> pMenus = new LinkedList();
        PMenu pMenu = null;
        Iterator var6 = menus.iterator();

        while(var6.hasNext()) {
            Menu m = (Menu)var6.next();
            pMenu = new PMenu();
            BeanUtils.copyProperties(m, pMenu);
            pMenus.add(pMenu);
        }

        if(roleid != null && !"".equals(roleid)) {
            List<Moudle> Moudles = this.moudleMapper.selectByPrimaryKey(roleid);
            Iterator var7 = Moudles.iterator();

            while(var7.hasNext()) {
                Moudle m = (Moudle)var7.next();
                pMenu = new PMenu();
                pMenu.setId(m.getMoudleid());
                pMenu.setPmoudleid(m.getMoudleid());
                pMenu.setText(m.getMoudlename());
                pMenu.setParentmoudle("parent");
                pMenus.add(pMenu);
            }
        }

        return pMenus;
    }

    public List<PMenu> getAllTreeMenu() {
        List<Menu> menus = this.menuMapper.selectTreeMenu();
        List<PMenu> pMenus = new LinkedList();
        PMenu pMenu = null;
        Iterator var5 = menus.iterator();

        while(var5.hasNext()) {
            Menu m = (Menu)var5.next();
            pMenu = new PMenu();
            BeanUtils.copyProperties(m, pMenu);
            pMenus.add(pMenu);
        }

        List<Moudle> Moudles = this.moudleMapper.findAll();
        Iterator var6 = Moudles.iterator();

        while(var6.hasNext()) {
            Moudle m = (Moudle)var6.next();
            pMenu = new PMenu();
            pMenu.setId(m.getMoudleid());
            pMenu.setPmoudleid(m.getMoudleid());
            pMenu.setText(m.getMoudlename());
            pMenu.setParentmoudle("parent");
            pMenus.add(pMenu);
        }

        return pMenus;
    }

    public int insertMenu(PMenu pmenu) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(pmenu, menu);
        boolean var3 = false;

        int iret;
        try {
            menu.setCreateTime(DateUtil.getInstance().getSqlTimeStamp());
            iret = this.menuMapper.insertMenu(menu);
        } catch (Exception var5) {
            logger.error("插入新菜单错误" + var5.toString());
            iret = -1;
        }

        return iret;
    }

    public boolean deleteMenu(PMenu pmenu) throws Exception {
//        int flag = false;
        Menu menu = new Menu();
        BeanUtils.copyProperties(pmenu, menu);
        int flag = this.menuMapper.deleteMenu(menu);
        return flag > 0;
    }

    public boolean menuEdit(PMenu pmenu) throws Exception {
//        int flag = false;
        Menu menu = new Menu();
        BeanUtils.copyProperties(pmenu, menu);
        if(menu.getStatus().intValue() == 0) {
            List<Menu> listData = this.menuMapper.getParentMoudleAllChildren(menu.getId());
            List<String> delList = new LinkedList();
            Iterator var7 = listData.iterator();

            while(var7.hasNext()) {
                Menu m = (Menu)var7.next();
                delList.add(m.getId());
            }

            this.menuMapper.updateChildrenByParent(delList);
        }

        if(menu.getStatus().intValue() == 1) {
            this.menuMapper.updateParentByChildren(menu);
        }

        int flag = this.menuMapper.menuEdit(menu);
        return flag > 0;
    }

    public List<PMenu> getAllMenuByMenuID(String menuId) {
        return this.menuMapper.selectMenusByMenuId(menuId);
    }
}
